// Import at top level
import { initializeApp } from "firebase/app";
import { getAnalytics, logEvent } from "firebase/analytics";

let analytics;

if (process.client) {
  const firebaseConfig = {
    apiKey: "AIzaSyCnDd6g0flFyGHRqIDcCX9KZFJV0E6qv7U",
    authDomain: "ins-web-47a23.firebaseapp.com",
    projectId: "ins-web-47a23",
    storageBucket: "ins-web-47a23.appspot.com",
    messagingSenderId: "418147404867",
    appId: "1:418147404867:web:78a037a4027e31507558f6",
    measurementId: "G-55TSTCV98M"
  };

  const app = initializeApp(firebaseConfig);
  analytics = getAnalytics(app);
  handleClick('guide_homepage_frist')
}

// Define handleClick at top level
export function handleClick(EventName, params) {
  //  console.log("click report", EventName, params);
  if (process.client) {
    // 上报点击事件
    logEvent(analytics, EventName, params);
  }
}

export function checkString(str) {
  let regex = new RegExp(
    "https?://(?:www.)?instagram.com/(stories|s|p|reel|tv)/.*",
    "g"
  );
  // 使用正则表达式检查字符串
  let match = regex.exec(str);
  // 根据匹配的内容返回不同的值
  if (match !== null) {
    switch (
      match[1] // we want to switch based on the captured group, which is at index 1 of the match array
    ) {
      case "stories":
      case "s":
        return "story";
      case "p":
        return "post";
      case "reel":
        return "reel";
      case "tv":
        return "tv";
      default:
        return "other";
    }
  }
  // 如果没有匹配的内容，则返回 'other'
  return "other";
}